package shapes.shapes2D.distribution.random 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import samples.BaseSample;
	import utils.GeomUtils;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Random extends BaseSample
	{
		private var operation:int = 0;
		private var totalOperations:int = 12;
		private var values:Vector.<int>;
		private var bd:BitmapData;
		private var bmp:Bitmap;
		private var txt:TextField;
		private var tf:TextFormat;
		
		public function Random() 
		{
			
			
			
			_stage.addEventListener( MouseEvent.MOUSE_DOWN, nextOperation );
			
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			addChild( txt );
			tf = new TextFormat( 'Verdana', 16, 0xFFFFFF );
			txt.x = 100;
			txt.y = 100 + sh-300 + 10;
			
			bd = new BitmapData( 250, 1, false, 0 );
			bmp = new Bitmap( bd );
			bmp.x =  100;
			bmp.y = txt.y +txt.height + 30;
			bmp.width = sw - 200;
			bmp.height = 20;
			addChild( bmp );
			
			nextOperation( null );
			
		}
		
		private function nextOperation(e:Event):void 
		{
			bd.fillRect( bd.rect, 0xFF0000 );
			
			var y:Number = 0;
			var maxY:Number = Number.NEGATIVE_INFINITY;
			var total:uint = bd.width * bd.height;
			
			var description:String  = '';
			
			values = new Vector.<int>();
			for ( var x:int = 0; x < bd.width; x++ )
			{
				values.push( 0 );
			}
			var i:int;
			var r:Number;
			for ( x = 0; x < bd.width; x++ )
			{
				i = 100;
				
				while ( i--)
				{
					switch( operation )
					{
						case 0:
							description = 'Math.random()';
							r =  Math.random();
						break;
						
						case 1:
							description = '.5 *  Math.random()';
							r =  .5 *  Math.random();
						break;
						
						case 2:
							description = '1 - ( .5 * Math.random() )';
							r =  1 - ( .5 * Math.random() );
						break;
						
						case 3:
							description = '( Math.random() > .5 ) ? (  .2 *  Math.random() ) : ( 1 - ( .2 *  Math.random() ) )';
							r = ( Math.random() > .5 ) ? (  .2 *  Math.random() ) : ( 1 - ( .2 *  Math.random() ) );
						break;
						
						case 4:
							description = 'Math.cos( .5 + ( Math.random() - .5 ) * 2 )';
							r = Math.cos( Math.random() );
						break;
						
						case 5:
							description = 'Math.sqrt( Math.random() )';
							r = Math.sqrt( Math.random() );
						break;
						
						case 6:
							description = '.5 * ( Math.random() + Math.random() )';
							r = .5 * ( Math.random() + Math.random() );;
						break;
						
						
						case 7:
								description = '.25 * ( Math.random() + Math.random() + Math.random() + Math.random() )';
							r = .25 * ( Math.random() + Math.random() + Math.random() + Math.random() );;
						
						break;
						
						
						case 8:
							description = 'r=Math.random()-Math.random()+Math.random()-Math.random(); r += (r<0.0) ? 4.0 : 0.0;r *= 0.25;';
							r=Math.random()-Math.random()+Math.random()-Math.random();
							r += (r<0.0) ? 4.0 : 0.0;
							r *= 0.25;
						break;
						
						
						case 9:
							description = '( Math.random() * Math.random() )';
							r =  ( Math.random() * Math.random() );
						break;
						
						
						case 10:
							description = 'Math.min( Math.random(), Math.random(), Math.random() )';
							r =  Math.min( Math.random(), Math.random(), Math.random() );
						break;
						
						case 11:
							description = 'Math.max( Math.random(), Math.random(), Math.random() )';
							r =  Math.max( Math.random(), Math.random(), Math.random() );
						break;
						
					}
					
					
					y = int( r * bd.width );
					
					values[ y ]++;
					if ( values[ y ] > maxY ) maxY = values[ y ];
				}
				
			}
			
			txt.text ='operation: ' + description;
			txt.setTextFormat( tf );
			graphics.clear();
			//graphics.lineStyle( 0, color );
			graphics.beginFill( 0 );
			graphics.drawRect( bmp.x, bmp.y, bmp.width, bmp.height );
			graphics.endFill();
			var color:uint;
			graphics.beginFill( 0 );
			for ( x = 0; x < values.length; x++ )
			{
				
				if ( values[x] == 0 )
				{
					color = 0x002266;
					values[x] = maxY;
				}
				else {
					
				y = GeomUtils.map( values[ x ], 0, maxY, 0, 0xFF );
				color = ( y << 16 | y << 8 | y )
				}
				graphics.beginFill(  color );
				graphics.drawRect( 	100 + x * ( bmp.width / values.length ), 
									100 + sh-300,
									bmp.width / values.length, 
									- GeomUtils.map( values[ x ], 0, maxY, 0, sh-300 ) );
				graphics.endFill();
				
				bd.setPixel( x, 0, color );
			}
			
			operation++;
			operation %= totalOperations;
			
		}
		
		
	}

}